/*
 * TAccessRightRequestPanel.java
 *
 * Created on April 25, 2007, 2:41 PM
 */

package cotex.working.gui;

import cotex.*;
import cotex.working.*;

import javax.swing.*;

/**
 *
 * @author  Ming
 */
public class TAccessRightRequestPanel extends javax.swing.JPanel {
 
    private TNode mNode = null;
    
    /**
     * Creates new form TAccessRightRequestPanel
     */
    public TAccessRightRequestPanel(TNode node) {
        
        mNode = node;
        
        initComponents();
        
        TWorkingNodeModel workingModel = (TWorkingNodeModel)mNode.getModel();
        
        mGrantAccessList.setModel( workingModel.getData().accessRightList.listModel);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        mGrantAccessList = new javax.swing.JList();

        setLayout(new java.awt.BorderLayout());

        setName("Access Right Request");
        mGrantAccessList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        mGrantAccessList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                mGrantAccessListMouseReleased(evt);
            }
        });

        add(mGrantAccessList, java.awt.BorderLayout.CENTER);

    }// </editor-fold>//GEN-END:initComponents

    private void mGrantAccessListMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mGrantAccessListMouseReleased
// TODO add your handling code here:
        if( evt.isPopupTrigger() ) {
            
            JMenuItem menuItem;
            JPopupMenu popup = new JPopupMenu();
            
            mGrantAccessList.setSelectedIndex( mGrantAccessList.locationToIndex( evt.getPoint() ) );
            java.awt.event.ActionListener listener = new java.awt.event.ActionListener() {
                
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    onGrantAccessMenuAction(e);
                }
            };
            
            if( mGrantAccessList.getSelectedValue() != null ) {
                
                menuItem = new JMenuItem("Approve");
                menuItem.setActionCommand("Approve");
                menuItem.addActionListener(listener);
                popup.add(menuItem);
                menuItem = new JMenuItem("Disapprove");
                menuItem.setActionCommand("Disapprove");
                menuItem.addActionListener(listener);
                popup.add(menuItem);
            }
            popup.show( evt.getComponent(), evt.getX(), evt.getY() );
        }
    }//GEN-LAST:event_mGrantAccessListMouseReleased
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList mGrantAccessList;
    // End of variables declaration//GEN-END:variables
    
    private void onGrantAccessMenuAction(java.awt.event.ActionEvent evt) {
        
        if( evt.getActionCommand().equals("Approve") ) {
        
            TAccessRightQuery query = (TAccessRightQuery)mGrantAccessList.getSelectedValue();
            mNode.execute( new TWorkingNodeModel.TReplyGrantAccessRightCmd(query, "true") );
        
        } else if( evt.getActionCommand().equals("Disapprove") ) {
        
            TAccessRightQuery query = (TAccessRightQuery)mGrantAccessList.getSelectedValue();
            mNode.execute( new TWorkingNodeModel.TReplyGrantAccessRightCmd(query, "false") );
        
        }
    }
}
